import leancloud
import Cust.leancloudapi
Getapi = Cust.leancloudapi.GetApi()
Classm = Getapi.getClass()
class ClassInfo:
    def getAllClass(self):
        query = Classm.query
        query.limit(1000)
        s = query.find()
        class_list = []
        for item in s:
            classid = item.id
            classname = item.get('ClassName')
            academy = item.get('Academy')
            classinfo = {'id':classid,'classname':classname,'academy':academy}
            class_list.append(classinfo)   
        return class_list
    def getClass(self,id):
        query = Classm()
        res = query.create_without_data(id)
        res.fetch()
        classname = res.get('ClassName')
        academy = res.get('Academy')
        classinfo = {'id':id,'academy':academy,'classname':classname}
        return classinfo
    def insertClass(self,name,academy):
        classinfo = Classm()
        classinfo.set('ClassName',name)
        classinfo.set('Academy',academy)
        try:
            classinfo.save()
            return 610
        except Exception as e:
            print e.message
            return 611
    def modifyClass(self,id,name,academy):
        updateclass = Classm.create_without_data(id)
        updateclass.set('ClassName',name)
        updateclass.set('Academy',academy)
        try:
            updateclass.save()
            return 630
        except Exception as e:
            print e.message
            return 631
    def delClass(self,id):
        delclass = Classm.create_without_data(id)
        try:
            self.delclassstu(delclass)
            delclass.destroy()
            return 620
        except Exception as e:
            print e.message
            return 621
    def delclassstu(self,id):
        stu = Getapi.getStudent().query
        res = stu.equal_to('ClassInfo',id).find()
        for foo in res:
            foo.destroy()
        